package cn.kgc.springbootvuefaq.service;

import cn.kgc.springbootvuefaq.mapper.ClassesFaqsMapper;
import cn.kgc.springbootvuefaq.mapper.FaqsMapper;
import cn.kgc.springbootvuefaq.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class FaqsServiceImpl implements FaqsService{
    @Resource
    FaqsMapper mapper1;
    @Resource
    ClassesFaqsMapper mapper2;

    @Override
    public PageInfo<ClassesFaqs> queryAll(Integer classId, Integer pageNum, Integer PageSize) {
        PageHelper.startPage(pageNum,PageSize);
        ClassesFaqsExample example=new ClassesFaqsExample();
        ClassesFaqsExample.Criteria criteria=example.createCriteria();
        if(classId!=0){
            criteria.andClassidEqualTo(classId);
            List<ClassesFaqs> list=mapper2.selectByExample(example);
            PageInfo<ClassesFaqs> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            List<ClassesFaqs> list=mapper2.selectByExample(null);
            PageInfo<ClassesFaqs> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public int Add(Faqs faqs) {
        return mapper1.insertSelective(faqs);
    }

    @Override
    public ClassesFaqs queryByid(Integer id) {
        ClassesFaqsExample example=new ClassesFaqsExample();
        example.createCriteria().andIdEqualTo(id);
        List<ClassesFaqs> list=mapper2.selectByExample(example);
        return list.get(0);
    }

    @Override
    public List<Faqs> queryByName(String title) {
        FaqsExample example=new FaqsExample();
        FaqsExample.Criteria criteria=example.createCriteria();
        criteria.andTitleEqualTo(title);
        List<Faqs> list=mapper1.selectByExample(example);
        return list;
    }
}
